﻿using Common.Communication;
using Common.Device.DeviceDb;
using Common.Device.Enums;
using Common.Device.Interfaces;
using Common.Tool.DataTools;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CeLiangYi.ShuZiDianQiao
{
    public class UTR2830ESerialPort : SerialPortBase, IShuZiDianQiao
    {
        public string Name { get; set; }
        public UTR2830ESerialPort(UseDevice device) : base(device.port, Convert.ToInt32(device.parameter), timeout: 1000)
        {
            Name = device.name;
        }
        public Result<double> GetDianRong(double vol, int channel = 1, CeLiangYiEnum.DianRongMode mode = CeLiangYiEnum.DianRongMode.CPD)
        {
            lock (this)
            {
                var result = new Result<double>();
                var write = new Result();
                var read = new Result<string>();
                AppendInfo(Name, $"设置测量模式{mode.ToString()}");
                write = Write(CmdSetType(mode.ToString()));
                if (write.IsSucceed == false)
                {
                    result.IsSucceed = false;
                    result.Err = "设置测量模式失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                read = WriteThenRead(CmdGetType);
                if (read.IsSucceed == false || !read.Value.Contains(mode.ToString()))
                {
                    result.IsSucceed = false;
                    result.Err = "设置测量模式失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                AppendInfo(Name, $"设置电平{vol}");
                write = Write(CmdSetVol(vol));
                if (write.IsSucceed == false)
                {
                    result.IsSucceed = false;
                    result.Err = "设置电平失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                read = WriteThenRead(CmdGetVol);
                if (read.IsSucceed == false || !double.TryParse(read.Value, out double v) || v != vol)
                {
                    result.IsSucceed = false;
                    result.Err = "设置电平失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                AppendInfo(Name, $"设置频率{100}");
                write = Write(CmdSetFreq(100));
                if (write.IsSucceed == false)
                {
                    result.IsSucceed = false;
                    result.Err = "设置频率失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                read = WriteThenRead(CmdGetFreq);
                if (read.IsSucceed == false || !double.TryParse(read.Value, out double f) || f != 100)
                {
                    result.IsSucceed = false;
                    result.Err = "设置频率失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                AppendInfo(Name, $"设置自动量程");
                write = Write(CmdAutoRange);
                if (write.IsSucceed == false)
                {
                    result.IsSucceed = false;
                    result.Err = "设置自动量程失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                read = WriteThenRead(CmdGetRange);
                if (read.IsSucceed == false || !read.Value.Contains("1"))
                {
                    result.IsSucceed = false;
                    result.Err = "设置自动量程失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                AppendInfo(Name, $"读取电容值");
                read = WriteThenRead(CmdFetch);
                if (read.IsSucceed == false || !double.TryParse(read.Value?.Split(',')[0], out double R))
                {
                    result.IsSucceed = false;
                    result.Err = "读取电容值失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                result.Value = R;
                AppendInfo(Name, DataConvert.ToUnit(R, "F3") + "F");
                return result.EndTime();
            }
        }
        public Result<double> GetDianGan(double vol, int channel = 1, CeLiangYiEnum.DianGanMode mode = CeLiangYiEnum.DianGanMode.LPD)
        {
            lock (this)
            {
                var result = new Result<double>();
                var write = new Result();
                var read = new Result<string>();
                AppendInfo(Name, $"设置测量模式{mode.ToString()}");
                write = Write(CmdSetType(mode.ToString()));
                if (write.IsSucceed == false)
                {
                    result.IsSucceed = false;
                    result.Err = "设置测量模式失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                read = WriteThenRead(CmdGetType);
                if (read.IsSucceed == false || !read.Value.Contains(mode.ToString()))
                {
                    result.IsSucceed = false;
                    result.Err = "设置测量模式失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                AppendInfo(Name, $"设置电平{vol}");
                write = Write(CmdSetVol(vol));
                if (write.IsSucceed == false)
                {
                    result.IsSucceed = false;
                    result.Err = "设置电平失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                read = WriteThenRead(CmdGetVol);
                if (read.IsSucceed == false || !double.TryParse(read.Value, out double v) || v != vol)
                {
                    result.IsSucceed = false;
                    result.Err = "设置电平失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                AppendInfo(Name, $"设置频率{100}");
                write = Write(CmdSetFreq(100));
                if (write.IsSucceed == false)
                {
                    result.IsSucceed = false;
                    result.Err = "设置频率失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                read = WriteThenRead(CmdGetFreq);
                if (read.IsSucceed == false || !double.TryParse(read.Value, out double f) || f != 100)
                {
                    result.IsSucceed = false;
                    result.Err = "设置频率失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                AppendInfo(Name, $"设置自动量程");
                write = Write(CmdAutoRange);
                if (write.IsSucceed == false)
                {
                    result.IsSucceed = false;
                    result.Err = "设置自动量程失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                read = WriteThenRead(CmdGetRange);
                if (read.IsSucceed == false || !read.Value.Contains("1"))
                {
                    result.IsSucceed = false;
                    result.Err = "设置自动量程失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                AppendInfo(Name, $"读取电感值");
                read = WriteThenRead(CmdFetch);
                if (read.IsSucceed == false || !double.TryParse(read.Value?.Split(',')[0], out double R))
                {
                    result.IsSucceed = false;
                    result.Err = "读取电感值失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                result.Value = R;
                AppendInfo(Name, DataConvert.ToUnit(R, "F3") + "H");
                return result.EndTime();
            }
        }
        string CmdSetVol(double vol) { return  "VOLT " + vol + "V\r\n"; }
        string CmdGetVol=> "VOLT?\r\n";
        string CmdSetFreq(int freq) { return "FREQ " + freq + "HZ\r\n"; }
        string CmdGetFreq => "FREQ?\r\n";
        string CmdSetType(string type) { return "FUNCtion:IMPedance " + type + "\r\n"; }
        string CmdGetType=> "FUNCtion:IMPedance?\r\n";
        string CmdAutoRange => "FUNCtion:IMPedance:RANGe:AUTO ON\r\n";
        string CmdGetRange => "FUNCtion:IMPedance:RANGe:AUTO?\r\n";
        string CmdFetch => "FETCH?\r\n";
    }
}
